const moment = require("../../public/utils/moment");

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.globalData.baseUrl,
    fileUrl: app.globalData.fileUrl,
    peiXunTypeList:[],
    gongKaiKeList:[{label:"是",value:"1"},{label:'否',value:"0"}],
    gongkaikeIndex:0,
    projectType:0,
    isShowDateTime:false,
    formData:{
      
    },
    peiXunLsList:[],
    chongMingList:[],
    peiXunKcList:[],
    teacher:'',
    teacherName:"",
    isShowChongMing:false,
    body:"",
    name:"",
    remark:"",
    course:[],
    courseName:"",

    bigSearchShow:false
  },
  


  init(params){
    const lessionInfo = wx.getStorageSync("lessionInfo")
    console.log('lessionInfo',lessionInfo,'11111',params);
    this.getExameType() 
    this.getCourseList() //所有课程
    this.setData({
      body:lessionInfo.body,
      name:lessionInfo.name,
      remark:lessionInfo.remark,
      course:lessionInfo.courseList,
      projectType:lessionInfo.projectType||params.projectType,
      gongkaikeIndex:lessionInfo.trainType,
      courseName:lessionInfo.course,
      teacher:lessionInfo.teacher
      // startTime:lessionInfo.startTime,
      // endTime:lessionInfo.endTime
    })

    
  },

  getCourseList(){
    const that = this;
    wx.request({
      url: that.data.baseUrl + '/v1/openApi/course/all',
      method: 'GET',
      data:"",
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        that.setData({
          peiXunKcList:res.data.rows
        })

        
      },fail(res) {
        
      },finally(){
      }
    });
  },

  searchSelect(e){
    const courseList = e.detail;
    const tempCouseName= []; 
    const tempCourseKey = []
    courseList.forEach(item=>{
      tempCouseName.push(item.name)
      tempCourseKey.push({key:item.id,value:item.name})
    })
    this.setData({
			courseName: tempCouseName.join(','),
      course:tempCourseKey
		})
  },


  courseChange(e){
    console.log('e',e);
    this.setData({
			bigSearchShow: true
		})
  },

  teacherChange(e){
    this.setData({
			teacherName: e.detail.value
		})
  },

  nameChange(e){
    this.setData({
			name: e.detail.value
		})
  },
  bodyChange(e){
    this.setData({
			body: e.detail.value
		})
  },
  remarkChange(e){
    this.setData({
			remark: e.detail.value
		})
  },

  closeMask(){
    this.setData({
      isShowChongMing:false
    })
  },

  doSubmit(){
    const startTime = this.selectComponent("#peiXunStart").data.time
    const endTime = this.selectComponent("#peiXunEnd").data.time
    if(!this.data.name){
      wx.showToast({
        title: "请输入培训标题",
        icon: 'none',
        duration: 2000
      });
      return
    }
    if(!this.data.body){
      wx.showToast({
        title: "请输入培训内容",
        icon: 'none',
        duration: 2000
      });
      return
    }
    if(!this.data.projectType){
      wx.showToast({
        title: "请输入培训类型",
        icon: 'none',
        duration: 2000
      });
      return
    }
    if(!startTime){
      wx.showToast({
        title: "请输入培训开始时间",
        icon: 'none',
        duration: 2000
      });
      return
    }
    if(!endTime){
      wx.showToast({
        title: "请输入培训结束时间",
        icon: 'none',
        duration: 2000
      });
      return
    }
    if(this.data.course.length==0){
      wx.showToast({
        title: "请选择课程",
        icon: 'none',
        duration: 2000
      });
      return
    }
    if(!this.data.gongkaikeIndex){
      wx.showToast({
        title: "请输入是否是公开课",
        icon: 'none',
        duration: 2000
      });
      return
    }

     
 
      this.doTrainAdd()
  },
  //重名选择
  selectLs(e){
    this.setData({
      teacher:e.currentTarget.dataset.item.id,
      isShowChongMing:false
    })
    this.doTrainAdd();
  },


  //添加信息
  doTrainAdd(){
    const that = this;
    const startTime = this.selectComponent("#peiXunStart").data
    const endTime = this.selectComponent("#peiXunEnd").data

    const startTimeFormat = startTime.timeFormat
    const endTimeFormat = endTime.timeFormat

    const courseList = [];
    this.data.course.forEach(item=>{
      courseList.push(item.key);
    })

    wx.showLoading({  
      title: '添加中', // 提示的内容  
      mask: true, // 是否显示透明蒙层，防止触摸穿透  
    });

    

    this.setData({
      formData:Object.assign({},{
        name:this.data.name,
        body:this.data.body,
        remark:this.data.remark,
        projectType:this.data.projectType,
        trainType:this.data.gongkaikeIndex,
        teacher:wx.getStorageSync('userinfo').siocSapNo,
        rangeTime:[startTimeFormat,endTimeFormat],
        course:courseList,
        status:1,
      })
    });

    wx.request({
      url: that.data.baseUrl + '/v1/openApi/trainer/add',
      method: 'POST',
      data:this.data.formData,
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        wx.hideLoading();

        if(res.data.code!=200){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });
          return
        }

        wx.navigateTo({
          url: '/pages/my-course/my-course',
        })
        
      }, fail(res) {
        
      },finally(){
        wx.hideLoading();
      }
    })


  },


  gongKaiKeChange(e){
    this.setData({
      gongkaikeIndex: e.detail.value
    })
  },

  peiXunLxChange(e){
    console.log('e',e);
    this.setData({
      projectType: e.detail.value
    })
  },

  getExameType(){
    const that = this;
    wx.request({
      url: that.data.baseUrl + '/v1/openApi/dic/data/examine',
      method: 'GET',
      data:"",
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        const tempArray = [];
        res.data.rows.forEach((item,index) => {
          tempArray.push(item.dictLabel)
        });

        that.setData({
          peiXunTypeList:res.data.rows
        })

        
      }, fail(res) {
        
      },finally(){
      }
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(params) {
    this.init(params);
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})